package com.loit.business.controller;                                                                                                      
                                                                                                                                             
import com.loit.business.entity.Student;                                                                                                  
import com.loit.business.service.StudentService;                                                                                          
import com.baomidou.mybatisplus.core.metadata.IPage;                                                                                                        
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;                                                                                                        
import com.loit.loitcommon.json.AjaxJson;                                                                                                        
import com.loit.loitcommon.utils.ExportExcel;                                                                                                        
import io.swagger.annotations.*;                                                                                                             
import org.slf4j.Logger;                                                                                                                     
import org.slf4j.LoggerFactory;                                                                                                              
import org.springframework.beans.factory.annotation.Autowired;                                                                               
import org.springframework.web.bind.annotation.*;                                                                                            
import springfox.documentation.annotations.ApiIgnore;                                                                                        
import javax.servlet.http.HttpServletResponse;                                                                                                                       
import java.util.List;                                                                                                                       
                                                                                                                                             
                                                                                                                                             


/**
 * [学生信息]接口
 * Created by lifh on 2022-12-26.
 **/
@RestController                                                                                                                              
@RequestMapping(value = "/api/v1/student")                                                                                                         
@Api(tags = "[学生信息]接口",  value = "StudentController")                                                                                              
public class StudentController {                                                                                                            
                                                                                                                                             
	/**                                                                                                                                      
	 * 日志工具                                                                                                                              
	 */                                                                                                                                      
	private Logger logger = LoggerFactory.getLogger(this.getClass());                                                                        
	                                                                                                                                         
	@Autowired                                                                                                                               
	private StudentService studentService;                                                                                                 
	                                                                                                                                         
                                                                                                                                             
	/**                                                                                                                                      
	 * 分页列表                                                                                                                          
	 */                                                                                                                                      
	@ApiOperation(value = "分页列表", notes = "分页列表")                                                                        
	@ApiImplicitParams({                                                                                                                     
	        @ApiImplicitParam(paramType = "query", name = "current", value = "当前页码", required = true, dataType = "Long"),  
	        @ApiImplicitParam(paramType = "query", name = "size", value = "分页大小", required = true, dataType = "Long"),
	        @ApiImplicitParam(paramType = "query", name = "tName", value = "学生名称", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tRank", value = "学生等级", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tSex", value = "学生性别 0女 1男", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tHobby", value = "兴趣爱好", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tClass", value = "班级编号", required = false, dataType = "String")
	})
	@RequestMapping(value = "/page", method = RequestMethod.GET)                                                                           
	public AjaxJson findPage(@ApiIgnore Student student, Long current, Long size) {                                  
		AjaxJson json = new AjaxJson();                                                                                                      
		IPage<Student> page = studentService.findListByPage(new Page<Student>(current, size), student);                                     
		json.put("page", page);                                                                                                            
		return json;                                                                                                                         
	}                                                                                                                                        
                                                                                                                                             
	/**                                                                                                                                      
	 * 详情                                                                                                                              
	 */                                                                                                                                    
	@ApiOperation(value = "详情", notes = "根据id获取详情信息")                                                                  
	@ApiImplicitParam(paramType = "path", name = "id", value = "学生信息id", required = true, dataType = "Long")                         
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)                                                                           
	public AjaxJson get(@PathVariable("id") Integer id) {                                                                              
		Student student = studentService.getById(id);                                                                                         
		AjaxJson json = new AjaxJson();                                                                                                      
		json.put("obj", student);                                                                                                         
		return json;                                                                                                                         
	}                                                                                                                                        
                                                                                                                                             
	/**                                                                                                                                      
	 * 保存                                                                                                                              
	 */                                                                                                                                      
	@ApiOperation(value = "保存", notes = "保存")                                                                                
	@RequestMapping(value = "/save", method = RequestMethod.POST)                                                                          
	public AjaxJson save(@RequestBody Student student) {                                                                                   
		AjaxJson json = new AjaxJson();                                                                                                      
		try {                                                                                                                                
			studentService.insert(student);                                                                                                
			json.setMsg("保存成功");                                                                                                       
		} catch (Exception e) {                                                                                                              
			e.printStackTrace();                                                                                                             
			return AjaxJson.returnExceptionInfo("保存异常");                                                                               
		}                                                                                                                                    
		return json;                                                                                                                         
	}                                                                                                                                        
	                                                                                                                                         
	/**                                                                                                                                      
	 * 修改                                                                                                                              
	 */                                                                                                                                      
	@ApiOperation(value = "修改", notes = "修改")                                                                                
	@RequestMapping(value = "/update", method = RequestMethod.POST)                                                                        
	public AjaxJson update(@RequestBody Student student) {                                                                                 
		AjaxJson json = new AjaxJson();                                                                                                      
		try {                                                                                                                                
			studentService.update(student);                                                                                                
			json.setMsg("修改成功");                                                                                                       
		} catch (Exception e) {                                                                                                              
			e.printStackTrace();                                                                                                             
			return AjaxJson.returnExceptionInfo("修改异常");                                                                               
		}                                                                                                                                    
		return json;                                                                                                                         
	}                                                                                                                                        
	                                                                                                                                         
	/**                                                                                                                                      
	 * 删除                                                                                                                              
	 */                                                                                                                                      
	@ApiOperation(value = "删除", notes = "删除")                                                                                
	@ApiImplicitParam(paramType = "path", name = "id", value = "学生信息id", required = true, dataType = "Integer")                         
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)                                                                        
	public AjaxJson delete(@PathVariable("id") Integer id) {                                                                               
		AjaxJson json = new AjaxJson();                                                                                                      
		try {                                                                                                                                
			studentService.delete(id);                                                                                  
			json.setMsg("删除成功!");                                                                                                      
		} catch (Exception e) {                                                                                                              
			e.printStackTrace();                                                                                                             
			return AjaxJson.returnExceptionInfo("删除失败" + e.getMessage());                                                              
		}                                                                                                                                    
		return json;                                                                                                                         
	}                                                                                                                                        
	                                                                                                                                         
    /**                                                                                                                                         
     * 导出                                                                                                                                         
     */                                                                                                                                         
    @ApiOperation(value = "导出", notes = "导出")                                                                                                                                         
    @ApiImplicitParams({                                                                                                                                         
	        @ApiImplicitParam(paramType = "query", name = "tName", value = "学生名称", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tRank", value = "学生等级", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tSex", value = "学生性别 0女 1男", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tHobby", value = "兴趣爱好", required = false, dataType = "String"),
	        @ApiImplicitParam(paramType = "query", name = "tClass", value = "班级编号", required = false, dataType = "String")
	})
    @GetMapping(value = "/export")                                                                                                                                         
    public void export(@ApiIgnore Student student, HttpServletResponse response) {                                                                                                                                         
        List<Student> list = studentService.findList(student);                                                                                                                                     
        try {                                                                                                                                     
            new ExportExcel(null, Student.class).setDataList(list).write(response, "学生信息数据.xlsx").dispose();                                                                                                                                 
        } catch (Exception e) {                                                                                                                                     
            e.printStackTrace();                                                                                                                                 
        }                                                                                                                                     
    }                                                                                                                                        
	                                                                                                                                         
}                                                                                                                                            
